import ports
import buf
import blit

splashscreen=buf.sym('splashscreen')
framebuffer =buf.abs(0xa0000, 0x10000)
textbuffer  =buf.abs(0xb8000, 4000)
savebuffer  =buf.bss(4000)
font0	    =buf.bss(8192)

textmode = 1

def exittext():
	savebuffer[:] = textbuffer
	set320x200x256()
	savefonts()
	global textmode; textmode = 0

def entertext():
	global textmode; textmode = 1
	restorefonts()
	set80x25()
	textbuffer[:] = savebuffer

def cleartext():
	if textmode:	tb = textbuffer
	else:		tb = savebuffer
	blit.fill(tb, 4000, 0,0, 4000,1, ' \015 \015 \015 \015')

def set640x480x16():
	ports.inb(0x3DA)
	ports.outb(0, 0x3C0)

	ports.writevec(0x3C4,0x3C5,  1,1,4,0,6)

	ports.outb(0xe3, 0x3C2)
	ports.writevec(0x3C4,0x3C5,  3)

	ports.outb(0x11, 0x3D4)
	ports.outb(   0, 0x3D5)

	ports.writevec(0x3D4,0x3D5,
                0x5f, 0x4f, 0x50, 0x82,  0x54, 0x80, 0x0b, 0x3e,
                0x00, 0x40, 0x00, 0x00,  0x00, 0x00, 0x00, 0x00,
                0xea, 0x8c, 0xdf, 0x28,  0x00, 0xe7, 0x04, 0xe3,
                0xff)

	ports.writevec(0x3CC,0x3CA,  1)
        ports.writevec(0x3CE,0x3CF,
                0x00, 0x00, 0x00, 0x00,  0x00, 0x00, 0x05, 0x0f,
                0xff)

	ports.inb(0x3DA)
	ports.writevec(0x3C0,0x3C0,
                0x00, 0x01, 0x02, 0x03,  0x04, 0x05, 0x06, 0x07,
                0x08, 0x09, 0x0A, 0x0B,  0x0C, 0x0D, 0x0E, 0x0F,
                0x01, 0x00, 0x0F, 0x00)

	ports.inb(0x3DA)
	ports.outb(0x20, 0x3C0)

def set80x25():
	ports.inb(0x3DA)
	ports.outb(0, 0x3C0)

	ports.writevec(0x3C4,0x3C5,  1,0,3,0,2)

	ports.outb(0x67, 0x3C2)
	ports.writevec(0x3C4,0x3C5,  3)

	ports.outb(0x11, 0x3D4)
	ports.outb(   0, 0x3D5)
	ports.writevec(0x3D4,0x3D5,
		0x5f, 0x4f, 0x50, 0x82,  0x55, 0x81, 0xbf, 0x1f,
		0x00, 0x4f, 0x0d, 0x0e,  0x00, 0x00, 0x00, 0x00,
		0x9c, 0x8e, 0x8f, 0x28,  0x1f, 0x96, 0xb9, 0xa3,
		0xff)

	ports.writevec(0x3CC,0x3CA,  1)
        ports.writevec(0x3CE,0x3CF,
		0x00, 0x00, 0x00, 0x00,  0x00, 0x10, 0x0e, 0x00,
		0xff)

	ports.inb(0x3DA)
	ports.writevec(0x3C0,0x3C0,
		0x00, 0x01, 0x02, 0x03,  0x04, 0x05, 0x06, 0x07,
		0x08, 0x09, 0x0A, 0x0B,  0x0C, 0x0D, 0x0E, 0x0F,
		0x0C, 0x00, 0x0F, 0x08)

	ports.inb(0x3DA)
	ports.outb(0x20, 0x3C0)


def set320x200x256():
	ports.inb(0x3DA)
	ports.outb(0, 0x3C0)

	ports.writevec(0x3C4,0x3C5,  3,1,0xf,0,0xe) # 6 for mode X
	
	ports.outb(0x63, 0x3C2)
	ports.writevec(0x3C4,0x3C5,  3)

	ports.outb(0x11, 0x3D4)
	ports.outb(   0, 0x3D5)
	ports.writevec(0x3D4,0x3D5,
		0x5f, 0x4f, 0x50, 0x82,  0x54, 0x80, 0xbf, 0x1f,
		0x00, 0x41, 0x00, 0x00,  0x00, 0x00, 0x00, 0x00,
		0x9c, 0x0e, 0x8f, 0x28,  0x40, 0x96, 0xb9, 0xa3,
					# 0x00, ..., 0xe3 for mode X
		0xff)

	ports.writevec(0x3CC,0x3CA,  1)
        ports.writevec(0x3CE,0x3CF,
		0x00, 0x00, 0x00, 0x00,  0x00, 0x40, 0x05, 0x0f,
		0xff)

	ports.inb(0x3DA)
	ports.writevec(0x3C0,0x3C0,
		0x00, 0x01, 0x02, 0x03,  0x04, 0x05, 0x06, 0x07,
		0x08, 0x09, 0x0A, 0x0B,  0x0C, 0x0D, 0x0E, 0x0F,
		0x41, 0x00, 0x0F, 0x00)

	ports.inb(0x3DA)
	ports.outb(0x20, 0x3C0)

def vgadefaultpalette():
	i = 0
	while i < 128:	# for i in range(256):
		ports.outb(i, 0x3C8)
		ports.outb(r[i], 0x3C9)
		ports.outb(g[i], 0x3C9)
		ports.outb(b[i], 0x3C9)
		i = i + 1

# initialize the palette

r = "\00\00\00\00\052\052\052\052\025\025\025\025\077\077\077\077\00\05\010\013\016\021\024\030\034\040\044\050\055\062\070\077\00\020\037\057\077\077\077\077\077\077\077\077\077\057\037\020\00\00\00\00\00\00\00\00\037\047\057\067\077\077\077\077\077\077\077\077\077\067\057\047\037\037\037\037\037\037\037\037\055\061\066\072\077\077\077\077\077\077\077\077\077\072\066\061\055\055\055\055\055\055\055\055\00\07\016\025\034\034\034\034\034\034\034\034\034\025\016\07\00\00\00\00\00\00\00\00\016\021\025\030\034\034\034\034\034\034\034\034\034\030\025\021\016\016\016\016\016\016\016\016\024\026\030\032\034\034\034\034\034\034\034\034\034\032\030\026\024\024\024\024\024\024\024\024\00\04\010\014\020\020\020\020\020\020\020\020\020\014\010\04\00\00\00\00\00\00\00\00\010\012\014\016\020\020\020\020\020\020\020\020\020\016\014\012\010\010\010\010\010\010\010\010\013\014\015\017\020\020\020\020\020\020\020\020\020\017\015\014\013\013\013\013\013\013\013\013\00\00\00\00\00\00\077"
g = "\00\00\052\052\00\00\025\052\025\025\077\077\025\025\077\077\00\05\010\013\016\021\024\030\034\040\044\050\055\062\070\077\00\00\00\00\00\00\00\00\00\020\037\057\077\077\077\077\077\077\077\077\077\057\037\020\037\037\037\037\037\037\037\037\037\047\057\067\077\077\077\077\077\077\077\077\077\067\057\047\055\055\055\055\055\055\055\055\055\061\066\072\077\077\077\077\077\077\077\077\077\072\066\061\00\00\00\00\00\00\00\00\00\07\016\025\035\034\034\034\034\034\034\034\034\025\016\07\016\016\016\016\016\016\016\016\016\021\025\030\034\034\034\034\034\034\034\034\034\030\025\021\024\024\024\024\024\024\024\024\024\026\030\032\034\034\034\034\034\034\034\034\034\032\030\026\00\00\00\00\00\00\00\00\00\04\010\014\020\020\020\020\020\020\020\020\020\014\010\04\010\010\010\010\010\010\010\010\010\012\014\016\020\020\020\020\020\020\020\020\020\016\014\012\013\013\013\013\013\013\013\013\013\014\015\017\020\020\020\020\020\020\020\020\020\017\015\014\00\00\00\00\00\00\077"
b = "\00\052\00\052\00\052\00\052\025\077\025\077\025\077\025\077\00\05\010\013\016\021\024\030\034\040\044\050\055\062\070\077\077\077\077\077\077\057\037\020\00\00\00\00\00\00\00\00\00\020\037\057\077\077\077\077\077\077\077\077\077\067\057\047\037\037\037\037\037\037\037\037\037\047\057\067\077\077\077\077\077\077\077\077\077\072\066\061\055\055\055\055\055\055\055\055\055\061\066\072\077\077\077\077\034\034\034\034\034\025\016\07\00\00\00\00\00\00\00\00\00\07\016\025\034\034\034\034\034\034\034\034\034\030\025\021\016\016\016\016\016\016\016\016\016\021\025\030\034\034\034\034\034\034\034\034\034\032\030\026\024\024\024\024\024\024\024\024\024\026\030\032\034\034\034\034\020\020\020\020\020\014\010\04\00\00\00\00\00\00\00\00\00\04\010\014\020\020\020\020\020\020\020\020\020\016\014\012\010\010\010\010\010\010\010\010\010\012\014\016\020\020\020\020\020\020\020\020\020\017\015\014\013\013\013\013\013\013\013\013\013\014\015\017\020\020\020\020\00\00\00\00\00\00\077"

#####################
# set up the palette
#####################

ports.inb(0x3DA)
ports.writevec(0x3C0,0x3C0,
		0x00, 0x01, 0x02, 0x03,  0x04, 0x05, 0x06, 0x07,
		0x08, 0x09, 0x0A, 0x0B,  0x0C, 0x0D, 0x0E, 0x0F)

vgadefaultpalette()

######################
# for now, these must be called in 320x200x256

def vgaunchain():
	ports.outb(4, 0x3CE); ports.outb(2, 0x3CF)	# plane 2
	ports.writevec(0x3C4,0x3C5,  3,1,4,0,6)		# unchained
	ports.outb(0x14, 0x3D4); ports.outb(0x00, 0x3D5)
	ports.outb(0x17, 0x3D4); ports.outb(0xe3, 0x3D5)

def vgarechain():
	ports.outb(4, 0x3CE); ports.outb(0, 0x3CF)	# plane 0
	ports.writevec(0x3C4,0x3C5,  3,1,0xf,0,0xe)	# chain-4
	ports.outb(0x14, 0x3D4); ports.outb(0x40, 0x3D5)
	ports.outb(0x17, 0x3D4); ports.outb(0xa3, 0x3D5)

def savefonts():
	vgaunchain()
	font0[:8192] = framebuffer[:8192]
	vgarechain()

def restorefonts():
	vgaunchain()
	framebuffer[:8192] = font0
	vgarechain()
